Ccay.Module.define("Ccay.example.ui.widget.xFormFunction", function (page, $S) {
	page.ready = function(){
		var studentModal = Ccay.Modal.create({
			modalName: "student",
			modalKey: "id",
			fields: [{field: "id", name:"User ID", type:"integer", renderLayout:true},
					 {field: "name", name:"User Name"},
					 {field: "sex", name: "User Sex", type: "bool", renderTmpl:"${student.sex}<a class='ccay-icon male' title='Male'></a>${student.sex_female}<a class='ccay-icon female' title='Female'></a>"},
					 {field: "sex_female", type: "bool", enable:false},
					 {field: "age", name:"User Age", type:"integer", rule: {range: [16, 80]}},
					 {field: "birthdayStart", name:"User Birthday", type:"date", layout:"range", renderTmpl:"${student.birthdayStart}${student.birthdayEnd}"},
					 {field: "birthdayEnd", type:"date", enable:false},
					 {field: "province", name: "User Province", type: "lookup", format: {code:"province", supportLang: true}}
			]
		});
		
		var xFormOption = {
			modal: studentModal,
			title: $i18n("ccay.common.button.search"),
			renderType: "search",
			operation: {
				search: function (e, form) {
					if (form.valid()) {
						var formValue = form.getValue();
						if (formValue.sex && formValue.sex_female) {
							delete formValue.sex;
							delete formValue.sex_female;
						} else {
							if (formValue.sex_female) {
								formValue.sex = 0;
								delete formValue.sex_female;
							}
						}
						formValue.birthdayStart && (formValue.birthdayStart = Ccay.Format.toDate(formValue.birthdayStart));
						formValue.birthdayEnd && (formValue.birthdayEnd = Ccay.Format.toDate(formValue.birthdayEnd));
						page.gridSearch(formValue);
					}
				}
			}
		};
		
		var studentModal02 = Ccay.Modal.create({
			modalName: "studentModal02",
			modalKey: "id",
			fields: [
				 {field: "id", name:"User ID", type: "hidden", renderType: "edit", renderLayout: false},
				 {field: "name", name:"User Name", rule: {required: true}},
				 {field: "sex", name: "User sex", type: "bool", format:{isIcon: true}, renderTmpl:"${studentModal02.sex}<a class='ccay-icon male' title='Male'></a>"},
				 {field: "age", name:"User Age", type:"integer"},
				 {field: "birthday", name:"User Birthday", type:"date"},
				 {field: "province", name: "User Province", type: "lookup", format: {code:"province", supportLang: true}},
				 {field: "blog", name: "User Blog", type: "longtext", rule: {url: true}, editorOp: {width: "100%"}, format: {height: "50px"}}
				 
			],
			rest:{
				createSingle: "services/ccay/demo/student/single",
				updateSingle: "services/ccay/demo/student/single",
				findSingle: "services/ccay/demo/student/single/${id}",
				deleteSingle: "services/ccay/demo/student/single/${id}"
			}
		});
		
		//绑定触发
		studentModal02.bindTrigger("deleteSingle", function () {
			page.gridSearch();
		});
		
		var toolOption = {
			items:[{
				type : "imgBtn",
				icon : "add",
				text : "ccay.common.button.add",
				click: function () {
					page.openModal("ccay.common.button.add", "edit", studentModal02);
				}
			}, {
				type : "imgBtn",
				icon : "del",
				text : "ccay.common.button.delete",
				attr: {
					id : "btnIconDel"
				}
			}]
		};

		var imgBtns = [{
			text: "ccay.workflow.label.edit", 
			type: "img", 
			imgtype: "edit",
			click: function (data, grid, row) { 
	    		page.openModal("ccay.workflow.label.edit", "edit", studentModal02, data.id);
	    	}
	    },{
	    	text: "ccay.security.label.groupViewUser",
	    	type: "img",
	    	imgtype: "view",
	    	click: function (data) { 
	    		page.openModal("ccay.security.label.groupViewUser", "view", studentModal02, data.id);
	    	}
	    },{
	    	text: "ccay.workflow.label.delete",
	    	type: "img", 
	    	imgtype: "del",
	    	click: function (data) { 
	    		Ccay.UI.confirm("ccay.common.message.confdel", function (result) {
	    			result && studentModal02.deleteSingle(data.id);
	    		});
	    	}
	    }];
		
		var gridOption = {
			minHeight: 200,
			maxHeight: 500,
			wrapHead: false,
		    columns: [
		    	{columnType: "select", multi: true},
		    	{columnType: "operate", width: 70, freezeable: true, btns: imgBtns},
		    	{field: studentModal.id, colOp: {width:80, freezeable: true, editable: false}},
		 		{field: studentModal.name, colOp: {width:90, header: "ccay.common.label.name", freezeable: true, rule: {required: true}}},
		 		{field: studentModal.sex, colOp: {width:70, header: "sSex", type: 'bool', format: {isIcon: true}}},
		 		{field: studentModal.age, colOp: {width:70, header: "sAge"}},
		 		{field: studentModal.province, colOp: {width:90, header: "sProvince"}},
		 		{field: 'birthday', width:110, header: "sBirthday", type: 'date'},
		 		{field: 'blog', width:130, header: "sBlog", renderer: 'ellipsis'},
		 		{field: 'lastUpdateDate', width:130, header: "ccay.common.label.lastUpdatedDate", type: 'datetime', editable: false}
		    ],
			ajax:{
			   	url: "services/ccay/demo/student/list/page/${pageSize}/${curPage}/${orderBy}"
			},
			operation: {
				del: {
		    		btn: '#btnIconDel',
		    		url: "services/ccay/demo/student/delete"
				}
			}
		};
		
		$S("#xFormFunctionDemo").ccayTab();
		
		//点击tab头后，才加载数据，只第一次生效
		$S("li[tabid='tab2']").one("click", function () {
			$S("#searchForm").xForm(xFormOption);
			$S("#operateTool").ccayToolbar(toolOption);
			$S("#iStudent").igrid(gridOption);
		});

	};
	
	page.gridSearch = function (param) {
		$S("#iStudent").manager().search(param);
	};
	
	page.operateTest = function (value) {
		alert(Ccay.DataHelper.toJsonStr(value));
	};
	
	page.openModal = function (title, renderType, studentModal, id) {
		var param = id,
			dialogOp = {
				content: "<div></div>",
				fnLoad: function (dialog) {
					this.getBody().children().xForm({
						modal: studentModal,
						layout: renderType == "edit" ? "form-Flow" : "form-Table",
						renderType: renderType,
						innerButton: false,
						showHead: false,
						param: param
					});
				}
			};
		if (renderType == "edit") {
			dialogOp.okClick = function (e) {
				var dialog = $(this).parent().parent().manager(),
					form = dialog.getBody().children().xForm();
				if (form.valid()) {
					var formValue = form.getValue(),
						modal = form.op.modal;
					!formValue.sex && (formValue.sex = 0);
					modal.setData(formValue);
					modal.saveSingle({
						success : function (result) {
							dialog.close();
							page.gridSearch();
						}
					});
				}
			}
		}
		else
			dialogOp.cancelClick = true;
		Ccay.UI.dialog(null, title, dialogOp);
	};
});